import axios, {
  type AxiosInstance,
  type AxiosResponse,
  type InternalAxiosRequestConfig
} from 'axios'

const http: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000
})

// 请求拦截
http.interceptors.request.use(
  // 成功
  (config: InternalAxiosRequestConfig) => {
    return config
  },
  // 失败
  (error) => {
    return Promise.reject(error)
  }
)

http.interceptors.response.use(
  (response: AxiosResponse) => {
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

export const get = <T>(url: string, params?: any) => {
  return http.get<T, HttpResponse<T>>(url, params)
}

export const post = <T>(url: string, data?: any) => {
  return http.post<T, HttpResponse<T>>(url, data)
}

export const del = <T>(url: string, data?: any) => {
  return http.delete<T, HttpResponse<T>>(url, data)
}
